import datetime

import pymysql

from db import DB
from tool import StringUtils
import json
from lolita.PostPublish import saveSina2Post

originDB = DB.getOriginDB()

sinaCollection = originDB.lolita_sina

mysqlCon = None
mysqlCur = None

sql = "INSERT INTO `lo_data_sina` (`text`,`images`,`author` ,`date`,`sina_id`) VALUES ( %s,%s, %s,%s,%s)"

querySql = "SELECT `sina_id` FROM `lo_data_sina` WHERE `sina_id`=%s"


def operateSina():
    mysqlCon = pymysql.Connect(
        host='rm-bp10684y5104946w7o.mysql.rds.aliyuncs.com',
        user='root',
        password="Xmtj6666",
        database='lolita',
        port=3306,
        charset='utf8mb4',
    )
    mysqlCur = mysqlCon.cursor()

    i = 1
    global sinaCollection
    alls = sinaCollection.find()
    for one in alls:
        isOriginal = one['isOriginal']
        if isOriginal:
            nick_name = one['nick_name']
            primary_key = one['primary_key']
            if nick_name != primary_key.split('_')[0]:
                continue
            date = one['date']
            sina_id = str(nick_name + date)
            mysqlCur.execute(querySql, sina_id)
            result = mysqlCur.fetchone()
            if result:
                print("已经添加了该微博！", result)
                break
            images = one['images']
            text = one['text']
            date = datetime.datetime.fromtimestamp(float(date) / 1000)
            text = StringUtils.dealSina(str(text))
            images = json.dumps(images)
            c = (text, images, nick_name, date, sina_id)
            mysqlCur.execute(sql, c)
            mysqlCon.commit()
            print("sina已经添加了%s条,[%s]" % (i, text))
            i += 1
            saveSina2Post(images, date, nick_name, text)
    mysqlCon.close()


if __name__ == "__main__":
    operateSina()

    # print("Pallida鸢尾蝶_1510287642000".split('_'))
    # 有表情的文本
    # s = "为可爱的绒绒打call！拍的蛛魇真的太太太好看啦！<imgclass=\"W_img_face\"render=\"ext\"src=\"\/\/img.t.sinajs.cn\/t4\/appstyle\/expression\/ext\/normal\/40\/pcmoren_tian_org.png\"title=\"[舔屏]\"alt=\"[舔屏]\"type=\"face\"\/><imgclass=\"W_img_face\"render=\"ext\"src=\"\/\/img.t.sinajs.cn\/t4\/appstyle\/expression\/ext\/normal\/40\/pcmoren_tian_org.png\"title=\"[舔屏]\"alt=\"[舔屏]\"type=\"face\"\/><imgclass=\"W_img_face\"render=\"ext\"src=\"\/\/img.t.sinajs.cn\/t4\/appstyle\/expression\/ext\/normal\/40\/pcmoren_tian_org.png\"title=\"[舔屏]\"alt=\"[舔屏]\"type=\"face\"\/>店主决定转发此微博并关注官博转发过一百抽两位姑娘送蜘蛛戒指啦！！！为姑娘们谋福利啊！！蛛魇定金过几天就截止啦！！店铺号：13243125QQ群：575832874加入QQ群可以解锁更多大图哦！​​​​"
    # print(StringUtils.replaceMoji(s.replace("\\", "")))
    # 显示全文
    # s = "_(:з」∠)_解释好心累直接去官网翻了好久……图一是官网LOGO，这篇公告刚好在中间不知道怎么截图到一起，图二是C婶原文，图三是国内翻译，来源看水印。BABY那条我当然也知道，那条买的是原版设计不是同人设计啊！_(:з」∠)_顺带一说C婶这篇公告也说了，她们的同人（比如JOJO），不要随便乱发，这个她 ​​​​...<a target=\"_blank\" href=\"//weibo.com/2460113044/Fe0Cwa3Hn\" class=\"WB_text_opt\" suda-uatrack=\"key=original_blog_unfold&value=click_unfold:4133389802397501:2460113044\" action-type=\"fl_unfold\" action-data=\"mid=4133389802397501&is_settop&is_sethot&is_setfanstop&is_setyoudao\">展开全文<i class=\"W_ficon ficon_arrow_down\">c</i></a>"
    # print(StringUtils.showAll(s))

    # s = "<a target=\"_blank\" render=\"ext\" extra-data=\"type=atname\" href=\"//weibo.com/n/Lo%E5%A8%98%E8%84%91%E6%B4%9E%E5%B0%8F%E5%A4%A9%E5%9D%91_lolita?from=feed&loc=at\" usercard=\"name=Lo娘脑洞小天坑_lolita\">@Lo娘脑洞小天坑_lolita</a> "
    # print(StringUtils.atSomeOne(s))

    # s = '<a suda-uatrack="key=minicard&value=pagelink_minicard_click" title="网页链接" href="http://t.cn/RWes8bD" alt="http://t.cn/RWes8bD" action-type="feed_list_url" target="_blank" ><i class="W_ficon ficon_cd_link">O</i>网页链接</a>'
    # print(StringUtils.dealNetUrl(s))

    # s = '<img class="W_img_face" render="ext" src="//img.t.sinajs.cn/t4/appstyle/expression/ext/normal/40/hearta_org.gif" title="[心]" alt=    "[心]" type="face" /><a target="_blank" render="ext" suda-uatrack="key=topic_click&value=click_topic" class="a_topic" extra-data="type=topic" href="//huati.weibo.com/k/%E5%A4%A9%E9%B9%85%E4%B9%8B%E5%BF%83?from=501">#天鹅之心#</a>&nbsp;&nbsp;JSK 三色人台图 <br><a target="_blank" render="ext" suda-uatrack="key=topic_click&value=click_topic" class="a_topic" extra-data="type=topic" href="//huati.weibo.com/k/LOLITA?from=501">#LOLITA#</a> <a target="_blank" render="ext" suda-uatrack="key=topic_click&value=click_topic" class="a_topic" extra-data="type=topic" href="//huati.weibo.com/k/LOLITA%E5%8E%9F%E5%88%9B%E7%B3%BB%E5%88%97?from=501">#LOLITA原创系列#</a> <a target="_blank" render="ext" suda-uatrack="key=topic_click&value=click_topic" class="a_topic" extra-data="type=topic" href="//huati.weibo.com/k/Lolita?from=501">#Lolita#</a><br><br>绝版设计图：<br><a suda-uatrack="key=minicard&value=pagelink_minicard_click" title="网页链接" href="http://t.cn/RWes8bD" alt="http://t.cn/RWes8bD" action-type="feed_list_url" target="_blank" ><i class="W_ficon ficon_cd_link">O</i>网页链接</a><br><br>三色样布：...<a target="_blank" href="//weibo.com/5786781540/FtESqe0s6" class="WB_text_opt" suda-uatrack="key=original_blog_unfold&value=click_unfold:4170686463338334:5786781540" action-type="fl_unfold" action-data="mid=4170686463338334&is_settop&is_sethot&is_setfanstop&is_setyoudao">展开全文<i class="W_ficon ficon_arrow_down">c</i></a>'
    # print(StringUtils.dealSina(s))

    # 微博店铺
    # s = "<imgsrc='\/\/img.t.sinajs.cn\/t4\/appstyle\/expression\/emimage\/ee858c.png'width=\"20px\"height=\"20px\">"
    # print(StringUtils.dealImg(s.replace("\\", "")))
